package com.lgq.cfw.service.impl;

import com.lgq.cfw.bean.TableMember;
import com.lgq.cfw.bean.example.TableMemberExample;
import com.lgq.cfw.bean.example.TableMemberExample.Criteria;
import com.lgq.cfw.bean.example.TableUserExample;
import com.lgq.cfw.dao.TableMemberMapper;
import com.lgq.cfw.service.MemberService;
import com.lgq.cfw.util.MD5Util;
import com.lgq.cfw.util.MyStringUtils;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Lin
 * @date 2019-02-15 21:16
 **/
@Service
public class MemberServiceImpl implements MemberService {

  @Autowired
  TableMemberMapper tableMemberMapper;

  @Override
  public boolean register(TableMember tableMember) {
    //对密码进行MD5加密
    tableMember.setUserpswd(MD5Util.digest(tableMember.getUserpswd()));
    //将账号赋值给username
    tableMember.setUsername(tableMember.getLoginacct());
    //实名认证情况，0代表为实名认证
    tableMember.setAuthstatus("0");
    //用户类型,0是普通用户
    tableMember.setUsertype("0");
    //row代表影响的行数，如果是1那么表示成功
    int row;
    //需要捕获数据库异常，如果发生异常直接返回false
    try{
      //插入数据
      row=tableMemberMapper.insertSelective(tableMember);
    }catch (Exception e){
      e.printStackTrace();
      return false;
    }
    return row==1;
  }

  /**
   * 验证重复账号
   * @param loginacct 账号
   * @return 布尔
   */
  @Override
  public boolean checkAccount(String loginacct) {
    TableMemberExample tableMemberExample=new TableMemberExample();
    Criteria criteria=tableMemberExample.createCriteria();
    criteria.andLoginacctEqualTo(loginacct);
    return tableMemberMapper.countByExample(tableMemberExample)==0;
  }

  /**
   * 登录
   * @param loginacct 账号
   * @param userpswd 密码
   * @return 布尔
   */
  @Override
  public boolean login(String loginacct, String userpswd) {
    TableMemberExample tableMemberExample=new TableMemberExample();
    Criteria criteria=tableMemberExample.createCriteria();
    criteria.andLoginacctEqualTo(loginacct);
    criteria.andUserpswdEqualTo(MD5Util.digest(userpswd));
    return tableMemberMapper.countByExample(tableMemberExample)==0;
  }
}
